package com.ziyun.pop.web.modules.storeFavouredPrice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.netflix.discovery.converters.Auto;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.popSysLog.entity.PopSysLogEntity;
import com.ziyun.pop.modules.popSysLog.service.PopSysLogService;
import com.ziyun.pop.modules.storeFavouredPrice.entity.StoreFavouredPriceDetailEntity;
import com.ziyun.pop.modules.storeFavouredPrice.entity.StoreFavouredPriceEntity;
import com.ziyun.pop.modules.storeFavouredPrice.service.StoreFavouredPriceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 门店价格账套
 *
 * @author jiangli
 * @email
 * @date 2018-09-25 15:04:37
 */
@RestController
@RequestMapping("storeFavouredPrice/storefavouredprice")
public class StoreFavouredPriceController {
    @Autowired
    private StoreFavouredPriceService storeFavouredPriceService;
    @Autowired
    private PopSysLogService popSysLogService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeFavouredPrice:storefavouredprice:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        PageUtils page = storeFavouredPriceService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/twblist")
    public R twblist(@RequestBody Map<String, Object> params) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        params.put("sellerId", sellerId);
        PageUtils page = storeFavouredPriceService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{favouredId}")
    @RequiresPermissions("storeFavouredPrice:storefavouredprice:info")
    public R info(@PathVariable("favouredId") Long favouredId) {
        StoreFavouredPriceEntity storeFavouredPrice = storeFavouredPriceService.getById(favouredId);

        return R.ok().put("storeFavouredPrice", storeFavouredPrice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeFavouredPrice:storefavouredprice:save")
    public R save(@RequestBody StoreFavouredPriceEntity storeFavouredPrice) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        storeFavouredPrice.setSellerId(sellerId);
        storeFavouredPrice.setCreateName(UserUtils.getCurentUser().getUsername());
        storeFavouredPriceService.save(storeFavouredPrice);
        return R.ok().put("favouredId",storeFavouredPrice.getFavouredId());
    }

    @RequestMapping("/twbsave")
    public R twbsave(@RequestBody StoreFavouredPriceEntity storeFavouredPrice) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        storeFavouredPrice.setSellerId(sellerId);
        storeFavouredPrice.setCreateName(UserUtils.getCurentUser().getUsername());
        storeFavouredPriceService.save(storeFavouredPrice);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeFavouredPrice:storefavouredprice:update")
    public R update(@RequestBody StoreFavouredPriceEntity storeFavouredPrice) {
        ValidatorUtils.validateEntity(storeFavouredPrice);
        //获取修改前的折扣率
        BigDecimal discountRate = storeFavouredPriceService.getById(storeFavouredPrice.getFavouredId()).getDiscountRate();
        storeFavouredPriceService.updateById(storeFavouredPrice);
        //增加日志
        PopSysLogEntity popSysLogEntity = new PopSysLogEntity();
        popSysLogEntity.setUsername(UserUtils.getCurentUser().getUsername());
        popSysLogEntity.setOperation("修改价格账套");
        popSysLogEntity.setMethod("账套ID:"+storeFavouredPrice.getFavouredId()+",商户ID:"+storeFavouredPrice.getSellerId());
        popSysLogEntity.setParams("折扣率修改前:"+discountRate+",修改后:"+storeFavouredPrice.getDiscountRate());
        popSysLogEntity.setTime(new Date().getTime());
        popSysLogEntity.setCreateDate(new Date());
        popSysLogService.save(popSysLogEntity);
        return R.ok();
    }

    @RequestMapping("/twbupdate")
    public R twbupdate(@RequestBody StoreFavouredPriceEntity storeFavouredPrice) {
        ValidatorUtils.validateEntity(storeFavouredPrice);
        storeFavouredPriceService.updateById(storeFavouredPrice);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storeFavouredPrice:storefavouredprice:delete")
    public R delete(@RequestBody Long[] favouredIds) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        storeFavouredPriceService.deleteByIds(favouredIds, sellerId);

        return R.ok();
    }

    /**
     * 使用折扣率
     */
    @PostMapping("/useRebate/{rebate}")
    public R useRebate(@RequestBody Long[] favouredIds, @PathVariable BigDecimal rebate) {
        return storeFavouredPriceService.useRebate(favouredIds, rebate);
    }

    /**
     * 启用/禁用
     */
    @PostMapping("/setStart/{favouredId}/{value}")
    public R setStart(@PathVariable Long favouredId, @PathVariable Boolean value) {
        return storeFavouredPriceService.setStart(favouredId, value);
    }

    /**
     * 账套详情
     */
    @GetMapping("/detailData/{favouredId}")
    public R detailData(@PathVariable Long favouredId) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        return storeFavouredPriceService.detailData(favouredId, sellerId);
    }

    /**
     * 保存阶梯价
     */
    @PostMapping("/saveStepPrice/{detailId}")
    public R saveStepPrice(@PathVariable Long detailId, @RequestBody String stepPriceData) {
        return storeFavouredPriceService.saveStepPrice(detailId, stepPriceData);
    }

    /**
     * 新增详情
     */
    @PostMapping("/saveDetail")
    public R saveDetail(@RequestBody StoreFavouredPriceDetailEntity storeFavouredPriceDetailEntity) {
        storeFavouredPriceDetailEntity.setCreateName(UserUtils.getCurentUser().getUsername());
        return storeFavouredPriceService.saveDetail(storeFavouredPriceDetailEntity);
    }

    /*
     *根据主键删除详情明细
     */
    @PostMapping("/deleteRow/{detailId}")
    public R deleteRow(@PathVariable Long detailId) {
        return storeFavouredPriceService.deleteRow(detailId);
    }

    /**
     * 修改详情
     */
    @PostMapping("/updateDetail")
    public R updateDetail(@RequestBody StoreFavouredPriceDetailEntity storeFavouredPriceDetailEntity) {
        storeFavouredPriceDetailEntity.setUpdateName(UserUtils.getCurentUser().getUsername());
        return storeFavouredPriceService.updateDetail(storeFavouredPriceDetailEntity);
    }

    /**
     * 查询当前商户的全部账套信息
     */
    @GetMapping("/getAllFavoured")
    public R getAllFavoured(){
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        List<StoreFavouredPriceEntity> storeFavouredPriceEntityList = storeFavouredPriceService.list(new QueryWrapper<StoreFavouredPriceEntity>().eq("seller_id",sellerId));
        return R.ok().put("storeFavouredPriceEntityList",storeFavouredPriceEntityList);
    }
}
